package com.iyundao.io.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.GiftService;
import com.iyundao.io.model.Gift;
import com.iyundao.io.model.SysUserProvider;
import com.iyundao.io.tools.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/gift")
public class GiftController {

    @Autowired
    private GiftService giftService;

    @ApiOperation(value="添加礼品", notes="添加礼品")
    @RequestMapping(value = "/addgift", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData insertGift(@RequestBody JSONObject params) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        Gift gift = JSON.toJavaObject(params, Gift.class);
        gift.setOrganizationid(sysUser.getSysDept().getSid());
        return giftService.insert(gift);

    }

    @ApiOperation(value="修改礼品状态", notes="修改礼品状态")
    @RequestMapping(value = "/updateflag", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData updateGiftFlag(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer)object.get("id");
        return giftService.updateFlag(id);

    }

    @ApiOperation(value="查询本组织礼品", notes="查询本组织礼品")
    @RequestMapping(value = "/selectbyorganizationid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<Gift> selectByOrganizationId(@RequestBody String params) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getSysDept().getSid();
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        return giftService.selectByOrganizationId(pagenum, pagesize, id);

    }

    @ApiOperation(value="查询单个礼品的详细信息", notes="查询单个礼品的详细信息")
    @RequestMapping(value = "/selectbyid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectbyId(@RequestBody JSONObject params) {
        Integer id = JSON.toJavaObject(params, Integer.class);
        return giftService.selectById(id);

    }

    @ApiOperation(value="修改礼品信息", notes="修改礼品信息")
    @RequestMapping(value = "/updategift", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData updateGift(@RequestBody JSONObject params) {
        Gift gift = JSON.toJavaObject(params, Gift.class);
        return giftService.updateGift(gift);

    }

}
